﻿/**
 * @fileOverview This file contains global functions used in this Widget Framework Library. 
 * Many of these functions are about communication or workflow event.
 * The other concerns the maintaining of the grafic state 
 * @author GUI group 
 * @version 1.0 alpha
 */

/* array that contains the current grafic state
 * (all widget rendered and actually in DOM)
 */
document.graficStateArray = new Array;

/**
* function that delete allpreviously declared widget in stage
*/
function cleanStage(){  
   for(widget in document.graficStateArray){
      if(document.graficStateArray[widget] instanceof BaseWidget){      
          var myW = document.graficStateArray[widget];
          myW.deleteMe();
          delete document.graficStateArray[widget];
      }
   }  
}

/**
* Utlity function for ajax comunication.
* <br/>Comunication() is developed in order to simplify the comunication between client side and server
* @param {String} server The ajax server URL.
* @param {String} par The data passed to server as input.
* @param {Function} [succCallback="function(){}"] The callback function that has to be execute in comunication success case.
* @param {Function} [errCallback="function(){}"] The callback function that has to be execute in comunication error case.
*
* @example
* comunication("myserver.aspx",{username=paolo,password=ciao},
* function(){alert("OK!")},function(){alert("NO BUONO!")})
*/

/** @ignore */
function AjaxSucceeded(result) {
    alert(result.d);
}

/** @ignore */
function AjaxFailed(result) {
    alert(result.status + ' ' + result.statusText);
}

var comunication = function (server, par, succCallback, errCallback) {
    $.ajax({
        type: "POST",
        url: server,
        data: par.toString(),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            //AjaxSucceeded(msg);
            succCallback(msg);
        },
        error: function(msg) {
            //AjaxFailed(msg);
            errCallback(msg);
        }
    });
}

//------------global utility function associated to the workflow ------------------

/**
* function used for throwing the event to workflow
* @param {String} e The event that has to be raised
*/
function raise(e){
    if(typeof e == "string")
    document.statechart.raise(e);
}

//------------widget gui for galoap framework-------------------

/**
* basic extend function
* @param {Object} B The object that extends 
* @param {Object} A The object that is extended
*/
function extend(B, A){
    function I(){}
    I.prototype = A.prototype;
    B.prototype = new I;
    B.prototype.constructor = B;
}